package com.ejlchina.common.bank;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ejlchina.common.util.StringUtils;

/**
 * 
 * 聚合四要素验证器
 * 
 * @author Troy.Zhou
 *
 */
public class JuheBankVerifier implements BankVerifier {

	
	Log log = LogFactory.getLog(JuheBankVerifier.class);
	
    //1:匹配 2:不匹配*
    static final int RESULT_MATCH = 1;
    static final int RESULT_NOT_MATCH = 2;
	
	
	static final String endpoint = "http://v.juhe.cn/verifybankcard4/query";
	
	
	private String appkey;
	
	
	public JuheBankVerifier() {
	}


	public JuheBankVerifier(String appkey) {
		super();
		this.appkey = appkey;
	}

/**
	//		名称	      	类型		必填	说明
	//		key     	string	是	您申请的key
	//		realname	string	是	姓名，需要utf8 Urlencode
	//		idcard	    string	是	身份证号码
	//		bankcard	string	是	银行卡卡号
	//		mobile	    string	是	手机号码
**/
	@Override
	public Result verify(String realName, String idCardNo, String bankPhone, String bankCardNo) {
		
		List<NameValuePair> list = new ArrayList<>();
		list.add(new BasicNameValuePair("key", appkey));
		list.add(new BasicNameValuePair("realname", realName));
		list.add(new BasicNameValuePair("idcard", idCardNo));
		list.add(new BasicNameValuePair("bankcard", bankCardNo));
		list.add(new BasicNameValuePair("mobile", bankPhone));
		
		String respResult = null;
		try {
			HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");

			HttpPost post = new HttpPost(endpoint) ; 
			post.setEntity(entity);
			
			HttpClient client = HttpClientBuilder.create().build();
			HttpResponse response = client.execute(post);
			
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != HttpStatus.SC_OK) {
				log.error("聚合银行四要素返回状态码不是200：" + statusCode);
				return Result.fail("聚合银行四要素返回状态码不是200！");
			}
			respResult = EntityUtils.toString(response.getEntity());
			
		} catch (Exception e) {
			throw new RuntimeException("聚合四要素认证请求异常", e);
		}
		
		log.info("认证返回结果: " + respResult);
		
		if (StringUtils.isBlank(respResult)) {
			log.error("认证返回结果为空！");
			return Result.fail("认证返回结果为空！");
		}
		
		JSONObject json = JSON.parseObject(respResult);
		
		if (json.getInteger("error_code") != 0) {
			return Result.fail(json.getString("reason")); 
		}
		JSONObject result = json.getJSONObject("result");
		
		if (result.getInteger("res") == RESULT_MATCH) {
			return Result.ok();
		}
		return Result.fail(result.getString("message"));
	}


	public String getAppkey() {
		return appkey;
	}

	public void setAppkey(String appkey) {
		this.appkey = appkey;
	}
	

}
